package com.landiao.ctrler.management;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.landiao.common.Constant;
import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.Querier;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.DateUtil;
import com.landiao.common.weixin.WxMpService;
import com.landiao.common.weixin.bean.WxMpTemplateData;
import com.landiao.common.weixin.bean.WxMpTemplateMessage;
import com.landiao.ctrler.BaseCtrler;
import com.landiao.entities.OrderDetail;
import com.landiao.entities.OrderInfo;
import com.landiao.entities.OrderSchedule;
import com.landiao.entities.SendTemplate;
import com.landiao.entities.Vip;
import com.landiao.service.OrderDetailService;
import com.landiao.service.OrderInfoService;
import com.landiao.service.OrderScheduleService;
import com.landiao.service.VipService;

/**
 * 功能说明:订单信息
 *
 */
@Controller
@RequestMapping("/management/orderinfo")
public class OrderInfoCtrler extends BaseCtrler{

	@Autowired
	private OrderDetailService detailService;
	
	@Autowired
	private OrderScheduleService scheduleService;
	
	@Autowired
	private OrderInfoService infoService;
	
	@Autowired
	@Qualifier("vipWxService")
	private WxMpService vipWxService;
	
	@Autowired
	private VipService vipService;

	@RequestMapping("index")
	public String index(Model model) {
		return viewName("index");
	}

	@ResponseBody
	@RequestMapping("page")
	public Page<OrderInfo> page(OrderInfo t,Querier querier){
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setShopId(this.getClientUser().getUser().getShopId());
		Page<OrderInfo> page = infoService.queryByPage(querier,t);
		return page;
	}
	@ResponseBody
	@RequestMapping(value = "updatestate/{id}/{state}", method = RequestMethod.POST)
	public ResultMsg updatestate(@PathVariable String id,@PathVariable String state) {
		try{
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setOrderInfoId(id);
			orderInfo.setState("10S");
			orderInfo.setCashPayWay(state);
			infoService.updateSelective(orderInfo);
			
			OrderInfo payOrderInfo = this.infoService.getById(id);
			if(payOrderInfo.getVipId()!=null){
				Vip vip = this.vipService.getById(payOrderInfo.getVipId());
				if(vip.getOperId()!=null){
					final String operId = vip.getOperId();
					final String orderNo = payOrderInfo.getOrderNo();
					final String payAmount = payOrderInfo.getShouldPay().toString()+"元";
					new Thread(new Runnable(){
						public void run(){  
							try {
								SendTemplate temp = Constant.VIP_TEMP_MAP.get(Constant.TEMP_ID_PAY_SUCCESS);
								if(temp!=null){
									WxMpTemplateMessage message = new WxMpTemplateMessage();
									message.setUrl(webSite+"weixin/vip/myEvaluate");
									message.setTouser(operId);

									message.setTemplate_id(temp.getTemplateId());
									message.setTopcolor("#FF0000");

									message.getData().put("first", new WxMpTemplateData("您好，您的订单已经支付成功"));
									message.getData().put("keyword1", new WxMpTemplateData(orderNo));
									message.getData().put("keyword2", new WxMpTemplateData(DateUtil.date2Str(new Date(), "yyyy年M月d日 HH:mm")));
									message.getData().put("keyword3", new WxMpTemplateData(payAmount));
									message.getData().put("keyword4", new WxMpTemplateData("现金支付"));
									message.getData().put("remark", new WxMpTemplateData("\n感谢您的惠顾，点击去评价"));

									vipWxService.templateSend(message);
								}
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}}).start();
				}
			}
			return ResultMsgFactory.success("保存成功");
		}catch(Exception e){
			e.printStackTrace();
			log.error("保存失败：{}",e.getMessage());
			return ResultMsgFactory.success("保存失败");
		}
	}
	@RequestMapping("detail/index/{id}")
	public String detailIndex(Model model,@PathVariable String id) {
		OrderInfo orderInfo = this.infoService.getById(id);
		model.addAttribute("orderInfo", orderInfo);
		return viewName("detailIndex");
	}
	
	@ResponseBody
	@RequestMapping("detail/page")
	public Page<OrderDetail> detailPage(OrderDetail detail,Querier querier){
		Page<OrderDetail> page = detailService.queryByPage(querier,detail);
		return page;
	}
	
	@RequestMapping("schedule/index/{id}")
	public String scheduleIndex(Model model,@PathVariable String id) {
		OrderInfo orderInfo = this.infoService.getById(id);
		model.addAttribute("orderInfo", orderInfo);
		return viewName("scheduleIndex");
	}
	
	@ResponseBody
	@RequestMapping("schedule/page")
	public Page<OrderSchedule> schedulePage(OrderSchedule schedule,Querier querier){
		Page<OrderSchedule> page = scheduleService.queryByPage(querier,schedule);
		return page;
	}
}
